<?php

Yii::import("ext.EAjaxUpload.qqFileUploader");

class EAjaxUploadAction extends CAction
{

    public function run()
    {  
        if (Yii::app()->request->IsPostRequest) {                               
            Yii::import("ext.EAjaxUpload.qqFileUploader");                            
            $allowedExtensions = Yii::app()->params['AllowImageType'];//array("jpg","jpeg","gif","exe","mov" and etc...
            $sizeLimit = Yii::app()->params['MaxImageSize'];// maximum file size in bytes
            $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);

            $attachmentFolder = Yii::app()->params['TmpImagesFolder'];
            if (! is_dir(Yii::getPathOfAlias('application') . '/../' . $attachmentFolder))
            {
                $r = mkdir(Yii::getPathOfAlias('application') . '/../' . $attachmentFolder, 0777, true);
                if (! $r)
                {
                    throw new CHttpException(501, 'Could not create folder '.$attachmentFolder);
                }
            }            
            $result = $uploader->handleUpload($attachmentFolder);
            $result=htmlspecialchars(json_encode($result), ENT_NOQUOTES);
            // it's array
            echo $result;
            Yii::app()->end();
        } 
        else {
            throw new CHttpException(400,Yii::t('Core','Invalid request. Please do not repeat this request again.'));
        }


        /* echo "khoa"   ;die;
        // list of valid extensions, ex. array("jpeg", "xml", "bmp")
        $allowedExtensions = array("jpg");
        // max file size in bytes
        $sizeLimit = 1 * 1024 * 1024;

        $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
        $result = $uploader->handleUpload('upload/');
        // to pass data through iframe you will need to encode all html tags
        $result=htmlspecialchars(json_encode($result), ENT_NOQUOTES);
        echo $result;   */

    }
}
